﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Web;
using System.Web.Mvc;
using System.Transactions;

namespace MobileTouchCafe.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Models.HomeData model = new Models.HomeData();

            using (NET_CafeEntities db=new NET_CafeEntities())
            {
                model.ViTriBan = db.L_ViTriBan.ToList().Select(
                    m => new SelectListItem()
                    {
                        Value = m.IdViTri.ToString(),
                        Text = m.TenViTri
                    }).ToList();
                
                //model.Ban = db.ExecuteStoreQuery<Entities.L_Ban>("exec dbo.sp_L_Ban 'GetAll'", null).ToList();
            }

            return View(model);
        }

        private List<Entities.eBh_BanHang_HangHoa> GetItems(int id)
        {
            List<Entities.eBh_BanHang_HangHoa> lst = new List<Entities.eBh_BanHang_HangHoa>();
            using (NET_CafeEntities db = new NET_CafeEntities())
            {
                lst =
                    db.Bh_BanHang_HangHoa.Where(m => m.IdBanHang == id).Select(
                        m => new Entities.eBh_BanHang_HangHoa()
                        {
                            IdBanHang = m.IdBanHang,
                            IdHangHoa = m.IdHangHoa,
                            IdDVT = m.IdDVT,
                            HangHoa = new Entities.eL_HangHoa()
                            {
                                IdHangHoa = m.L_HangHoa.IdHangHoa,
                                MaHang = m.L_HangHoa.MaHang,
                                TenHang = m.L_HangHoa.TenHang,
                                HinhAnh = m.L_HangHoa.HinhAnh
                            },
                            DVT = new Entities.eL_DVT()
                            {
                                IdDVT = m.L_DVT.IdDVT,
                                TenDVT = m.L_DVT.TenDVT
                            },
                            SoLuong = m.SoLuong,
                            DonGia = m.DonGia,
                            ThanhTien = m.ThanhTien,
                            PT_CK = m.PT_CK,
                            Tien_CK = m.Tien_CK,
                            TongTien = m.TongTien
                        }).ToList();
            }
            return lst;
        }

        public JsonResult ViewDetail(int id)
        {
            Models.Md_BanHang model = new Models.Md_BanHang();
            
            using (NET_CafeEntities db = new NET_CafeEntities())
            {
                model.BanHang = db.ExecuteStoreQuery<Entities.eBh_BanHang>("exec dbo.SP_Bh_BanHang @Command,0,null,@IdBan", new SqlParameter("Command", "LoadDataByTableID"), new SqlParameter("IdBan", id)).FirstOrDefault();

                model.NhomHang = db.L_NhomHang.Select(
                    m => new Entities.eL_NhomHang() { 
                        IdNhom=m.IdNhom,
                        MaNhom=m.MaNhom,
                        TenNhom=m.TenNhom,
                        HinhAnh=m.HinhAnh,
                        GhiChu=m.GhiChu
                    }).ToList();

                model.HangHoa = db.Sp_Menu_GetAll().Select(
                    m => new Entities.eL_HangHoa()
                    {
                        IdHangHoa=m.IdHangHoa,
                        MaHang=m.MaHang,
                        TenHang=m.TenHang,
                        IdDVT=m.IdDVT,
                        TenDVT=m.TenDVT,
                        IdNhom=m.IdNhom.Value,
                        HinhAnh=m.HinhAnh,
                        DonGia=m.DonGia.Value
                    }).ToList();
            }

            if (!object.Equals(model.BanHang, null))
            {
                model.BanHang_HangHoa = GetItems(model.BanHang.IdBanHang);
            }
            else
            {
                model.BanHang = new Entities.eBh_BanHang();
                model.BanHang.IdBan = id;
                model.BanHang.Ngay = DateTime.Now;
            }

            string data = Controllers.RenderPartialView.Render(this, "ViewDetail", model);
            return Json(new { Success = true, data = data }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDetail(int id)
        {
            return Json(new { Success = true, data=GetItems(id) }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetTableAndLocation()
        {
            Models.HomeData model = new Models.HomeData();
            using (NET_CafeEntities db = new NET_CafeEntities())
            {
                model.ViTriBan = db.L_ViTriBan.ToList().Select(
                    m => new SelectListItem()
                    {
                        Value = m.IdViTri.ToString(),
                        Text = m.TenViTri
                    }).ToList();

                model.Ban = db.ExecuteStoreQuery<Entities.eL_Ban>("exec dbo.sp_L_Ban 'GetAll'", null).OrderBy(m => m.IdViTri).ToList();
            }
            return Json(new { Success = true, data = model }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetAllTable()
        {
            Models.HomeData model = new Models.HomeData();

            using (NET_CafeEntities db = new NET_CafeEntities())
            {
                model.Ban = db.ExecuteStoreQuery<Entities.eL_Ban>("exec dbo.sp_L_Ban 'GetAll'", null).OrderBy(m => m.IdViTri).ToList();
            }

            return Json(new { Success = true, data = model.Ban }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateData(int IdBanHang, int IdBan, string Ngay,
                                    decimal ThanhTien,int PT_CK,decimal TienCK,
                                    decimal TongTien,decimal KhachTra, List<string> Detail)
        {
            string err = "";

            using (NET_CafeEntities db = new NET_CafeEntities())
            {
                try
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        db.Connection.Open();

                        System.Data.Objects.ObjectParameter id = new System.Data.Objects.ObjectParameter("IdBanHang", IdBanHang);

                        db.Sp_Bh_BanHang("Update", id, DateTime.ParseExact(Ngay, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                            IdBan, ThanhTien, PT_CK, TienCK, TongTien, KhachTra, "", 0);

                        int new_id = (IdBanHang == 0) ? Convert.ToInt32(id.Value) : IdBanHang;

                        db.Sp_Bh_BanHang_HangHoa("Delete", new_id, 0, 0, 0, 0, 0, 0, 0, 0);

                        foreach (string row in Detail)
                        {
                            string[] arr = row.Split('#');
                            db.Sp_Bh_BanHang_HangHoa("Insert", new_id, Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]),
                                Convert.ToDecimal(arr[2]), Convert.ToDecimal(arr[3]), Convert.ToDecimal(arr[2]) * Convert.ToDecimal(arr[3]),
                                0, 0, Convert.ToDecimal(arr[4]));
                        }

                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    err = ex.Message;
                }
                finally
                {
                    if (db.Connection.State == System.Data.ConnectionState.Open)
                        db.Connection.Close();
                }
            }

            return Json(new { Success = (err.Length > 0) ? false : true, Err = err }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult OnDelete(int IdBanHang)
        {

            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }
    }
}
